package cn.zhibang.sannuo.web.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author AdminController
 * @version 1.0.0
 * @ClassName ShiroConfig.java
 * @Description TODO
 * @createTime 2021年03月30日 09:47:00
 */
@Configuration
public class ShiroConfig {

    @Bean
    public Realm getRealm(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("MD5");
        matcher.setHashIterations(1024);
        AdminRealm adminRealm = new AdminRealm();
        adminRealm.setCredentialsMatcher(matcher);
        return adminRealm;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("getRealm") Realm realm){
        DefaultWebSecurityManager webSecurityManager = new DefaultWebSecurityManager();
        webSecurityManager.setRealm(realm);
        return webSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);


        Map<String, String> map = new LinkedHashMap<>();
        map.put("/index", "authc");
        map.put("/userDetection/*", "authc");
        map.put("/report/*", "authc");
        map.put("/pharmacy/*", "authc");
        map.put("/account/*", "authc");
        map.put("/maintenance/*", "authc");
        map.put("/mycenter/*", "authc");

        bean.setFilterChainDefinitionMap(map);

        bean.setLoginUrl("/login");

        return bean;
    }

    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
